
package com.yimixian.app.order;

import com.yimixian.app.R;
import com.yimixian.app.util.CustomViewUtils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

import java.util.HashMap;

public class PaymentMethodItemView extends FrameLayout {

    @InjectView(R.id.pay_method_text)
    TextView mPayMethodNameText;
    @InjectView(R.id.balance_text)
    TextView mBalanceText;
    @InjectView(R.id.pay_method_image)
    ImageView mPayMethodImage;
    @InjectView(R.id.check_image)
    ImageView mCheckImage;

    private String mPayMethodName;
    private boolean mChecked = false;

    public PaymentMethodItemView(Context context) {
        super(context);
        initDeliveryModeView();
    }

    public PaymentMethodItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initDeliveryModeView();
    }

    public PaymentMethodItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initDeliveryModeView();
    }

    private void initDeliveryModeView() {
        LayoutInflater.from(getContext()).inflate(R.layout.pay_method_item_view, this);
        ButterKnife.inject(this);
        CustomViewUtils.setViewBackground(this,
                getResources().getDrawable(R.drawable.settings_item_bg));
    }

    public void bind(String payMethodName, String remainingBalance,
            HashMap<String, String> mPaymentMethodTitles) {
        mPayMethodName = payMethodName;
        switch (mPayMethodName) {
            case (ConfirmOrderActivity.PAYMENT_METHOD_WECHAT):
                if (mPaymentMethodTitles != null
                        && mPaymentMethodTitles.get(mPayMethodName) != null) {
                    mPayMethodNameText.setText(mPaymentMethodTitles.get(mPayMethodName));
                } else {
                    mPayMethodNameText.setText("微信支付(推荐)");
                }
                mPayMethodImage.setImageResource(R.drawable.wechat_pay_icon);
                break;
            case (ConfirmOrderActivity.PAYMENT_QQ_WALLET):
                if (mPaymentMethodTitles != null
                    && mPaymentMethodTitles.get(mPayMethodName) != null) {
                    mPayMethodNameText.setText(mPaymentMethodTitles.get(mPayMethodName));
                } else {
                    mPayMethodNameText.setText("QQ钱包支付");
                }
                mPayMethodImage.setImageResource(R.drawable.icon_qq_wallet);
                break;
            case (ConfirmOrderActivity.PAYMENT_METHOD_ALIPAY):
                if (mPaymentMethodTitles != null
                        && mPaymentMethodTitles.get(mPayMethodName) != null) {
                    mPayMethodNameText.setText(mPaymentMethodTitles.get(mPayMethodName));
                } else {
                    mPayMethodNameText.setText("支付宝支付");
                }
                mPayMethodImage.setImageResource(R.drawable.zhifubao_pay_icon);
                break;
            case (ConfirmOrderActivity.PAYMENT_METHOD_BALANCE):
                if (mPaymentMethodTitles != null
                        && mPaymentMethodTitles.get(mPayMethodName) != null) {
                    mPayMethodNameText.setText(mPaymentMethodTitles.get(mPayMethodName));
                } else {
                    mPayMethodNameText.setText("余额支付");
                }
                mPayMethodImage.setImageResource(R.drawable.balance_pay_icon);
                mBalanceText.setText("账户余额:"
                        + String.format("%.2f", Double.valueOf(remainingBalance)));
                setEnabled(false);
                break;
            default:
                mPayMethodNameText.setText("微信支付(推荐)");
                mPayMethodImage.setImageResource(R.drawable.wechat_pay_icon);
        }
    }

    public String getPayMethodName() {
        return mPayMethodName;
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if (!enabled) {
            if (!mChecked) {
                setCheckImageVisibility(View.GONE);
            } else {
                setCheckImageVisibility(View.VISIBLE);
            }
        } else {
            setCheckImageVisibility(View.VISIBLE);
        }

    }

    public void setCheckImageVisibility(int visibility) {
        mCheckImage.setVisibility(visibility);
    }

    public void setChecked(boolean checked) {
        mChecked = checked;
        mCheckImage.setImageResource(checked ? R.drawable.confirm_order_checked_image :
                R.drawable.confirm_order_unchecked_image);
    }
}
